﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;
using System.Web.UI.HtmlControls;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class MP_Jobsites : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IJobSiteBLL job = new JobSiteBLL();
        private IWorkAreaBLL area = new WorkAreaBLL();
        private IJobSiteTypeBLL typeBLL = new JobSiteTypeBLL();
        private IJobSiteCommandBLL cmd = new JobSiteCommandBLL();
        private IPositionBLL positionBLL = new PositionBLL();
        private ITreeViewBLL treeBLL = new TreeViewBLL();
        private IMasDistrictBLL districtBLL = new MasDistrictBLL();
        private IMasCountryBLL countryBLL = new MasCountryBLL();
        private IMasCityBLL cityBLL = new MasCityBLL();
        private DropDownListBLL ddlBLL = new DropDownListBLL();

        Main main = new Main();

        private JobSiteTypeInfo GetTypeInfo(Nullable<int> org)
        {
            IList<JobSiteTypeInfo> typeInfo = typeBLL.ListJobSiteTypes(org);
            if (typeInfo.Count > 0)
                return typeInfo[0];
            return null;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            JobSiteTypeInfo typeInfo = GetTypeInfo((Nullable<int>) Int32.Parse(Request.QueryString["org"]));

            Page.Title = "ระบบบริหารทรัพยากรบุคคล " + (String.IsNullOrEmpty(typeInfo.Name) ? "" : " - " + typeInfo.Name);

            if (!IsPostBack)
            {
                LoadDDLJobsiteUpperSeq();
                //LoadDDLJobsiteExternalSeq();
                LoadDDLWorkAreaSeq();
                LoadDDLJobsiteLevelSeq();
                LoadDDLJobsiteHeadPosSeq();
                LoadDDLCountrySeq();


                string captionRoot = Request.QueryString["org"] == "1"
                                         ? GetLocalResourceObject("treeViewRootCaptionJobSite2") + " : "
                                         : GetLocalResourceObject("treeViewRootCaptionJobSite1") + " : ";

                Session["HeaderName"] = typeInfo.Name + " : " + captionRoot;
                
                LoadTreeView();

                if (Session["TreeNodeValue"] != null && Request.QueryString["org"] == Convert.ToString(Session["QueryString"]))
                {
                    try
                    {
                        RadTreeView1.FindNodeByValue(Session["TreeNodeValue"].ToString()).Selected = true;
                        RadTreeView1.SelectedNode.Expanded = true;
                        RadTreeView1.SelectedNode.ExpandParentNodes();
                    } catch
                    {
                    }
                }
                else if (RadTreeView1.Nodes.Count > 0)
                {
                    RadTreeView1.Nodes[0].Selected = true;
                    Session["TreeNodeValue"] = RadTreeView1.Nodes[0].Value;
                }

                Session["QueryString"] = Request.QueryString["org"];
                OnUpdateCmdUI("");
            } 

            tblJobSiteHeadPosLabel.Visible = typeInfo.Sequence == 2;
            tblJobSiteHeadPosInput.Visible = typeInfo.Sequence == 2;
        }

        private void BindDataControl()
        {
            DataSet ds = job.LoadJobSites(Session["TreeNodeValue"].ToString(), "", "", "");

            DataRowCollection drc;

            if (ds.Tables.Count > 0)
            {
                drc = ds.Tables[0].Rows;
                if (drc.Count > 0)
                {
                    ddlJobSiteRef.Visible = Request.QueryString["org"].ToString() == SystemInfo._JobsiteInternal;
                    lblJobsiteRef.Visible = ddlJobSiteRef.Visible;
                    //reqddlJobSiteRef.Enabled = ddlJobSiteRef.Visible;

                    lblJobsiteSeq.Text = drc[0]["JOB_SITE_SEQ"].ToString();
                    txtJobsiteRef.Text = drc[0]["JOB_SITE_REF"].ToString();
                    txtJobsiteGFMIS.Text = drc[0]["gfmis_code"].ToString();
                    txtJobsiteName.Text = drc[0]["JOB_SITE_NAME"].ToString();
                    txtJobsiteNameShort.Text = drc[0]["JOB_SITE_NAME_SHORT"].ToString();
                    txtJobsiteNameEng.Text = drc[0]["JOB_SITE_NAME_ENG"].ToString();
                    txtNameEngShort.Text = drc[0]["JOB_SITE_NAME_ENG_SHORT"].ToString();
                    //txtDateStart.SetValue(drc[0]["DATE_START"]);
					txtJobDescription.Text = drc[0]["JOB_DESCRIPTION"].ToString();
					txtAddress.Text = drc[0]["ADDRESS"].ToString();
					txtAddress2.Text = drc[0]["ADDRESS2"].ToString();
					txtWebsite.Text = drc[0]["WEBADDRESS"].ToString();
                    txtPostalCode.Text = drc[0]["POSTAL_CODE"].ToString();
                    txtTel.Text = drc[0]["TEL"].ToString();
                    txtFax.Text = drc[0]["FAX"].ToString();
                    txtRemarks.Text = drc[0]["REMARKS"].ToString();

                    ddlWorkAreaSeq.SelectedValue = drc[0]["WORK_AREA_SEQ"].ToString(); //Convert.ToString(DataBinder.Eval(DataItem, "work_area_seq"));
                    ddlJobsiteUpperSeq.SelectedValue = drc[0]["upper_job_site_seq"].ToString(); //Convert.ToString(DataBinder.Eval(DataItem, "upper_job_site_seq"));
                    if (ddlJobSiteRef.Visible)
                    {
                        ddlJobSiteRef.SelectedValue = drc[0]["ref_job_site_seq"].ToString(); //Convert.ToString(DataBinder.Eval(DataItem, "ref_job_site_seq"));
                    }
                    
                    ddlJobsiteLevelSeq.SelectedValue = drc[0]["JOB_SITE_LEVEL_SEQ"].ToString(); //Convert.ToString(DataBinder.Eval(DataItem, "job_site_level_seq"));

                    if (!ddlJobsiteLevelSeq.Visible)
                        SetJobSiteLevelName(ddlJobsiteLevelSeq.Text);
                    else
                        SetJobSiteLevelName(lblLevel.Text);

                    ddlCountrySeq.SelectedValue = drc[0]["COUNTRY_SEQ"].ToString(); //Convert.ToString(DataBinder.Eval(DataItem, "country_seq"));

                    LoadDDLProvinceSeq(ddlCountrySeq.SelectedValue);

                    if (drc[0]["PROV_SEQ"].ToString().Length > 0)
                    {
                        cmbProvince.SelectedValue = drc[0]["PROV_SEQ"].ToString();// Convert.ToString(DataBinder.Eval(DataItem, "prov_seq"));
                        LoadDDLDistrictSeq(cmbProvince.SelectedValue);
                        ddlDistrictSeq.SelectedValue = drc[0]["DISTRICT_SEQ"].ToString(); // Convert.ToString(DataBinder.Eval(DataItem, "district_seq"));
                    }

                    if (drc[0]["head_pos_seq"].ToString().Length > 0)
                    {
                        ddlJobSiteHeadPosSeq.SelectedValue = drc[0]["head_pos_seq"].ToString();
                    }
                    else
                    {
                        ddlJobSiteHeadPosSeq.SelectedValue = null;
                    }

                }
            }


        }

        private void ClearControl()
        {
            DataSet ds = job.LoadJobSites(Session["TreeNodeValue"].ToString(), "", "", "");
            
            DataRowCollection drc;

            //{
            //    main.ClearComboBox(ref ddlJobsiteUpperSeq);
            //    main.ClearComboBox(ref ddlJobSiteRef);
            //    main.ClearComboBox(ref ddlWorkAreaSeq);
            //    main.ClearComboBox(ref ddlJobsiteLevelSeq);
            //    main.ClearComboBox(ref ddlJobSiteHeadPosSeq);
            //    main.ClearComboBox(ref ddlCountrySeq);
            //    main.ClearComboBox(ref ddlDistrictSeq);
            //    main.ClearComboBox(ref cmbProvince);
            //}

            if (ds.Tables.Count > 0)
            {
                drc = ds.Tables[0].Rows;
                if (drc.Count > 0)
                {
                    //LoadDDLJobsiteUpperSeq();
                    //LoadDDLJobsiteExternalSeq();
                    //LoadDDLWorkAreaSeq();
                    //LoadDDLJobsiteLevelSeq();
                    //LoadDDLCountrySeq();
                    //LoadDDLJobsiteHeadPosSeq();
                    ddlJobSiteRef.Visible = Request.QueryString["org"].ToString() == SystemInfo._JobsiteInternal;
                    lblJobsiteRef.Visible = ddlJobSiteRef.Visible;
                    //reqddlJobSiteRef.Enabled = ddlJobSiteRef.Visible;

                    lblJobsiteSeq.Text = drc[0]["JOB_SITE_SEQ"].ToString();
                    txtJobsiteRef.Text = "";
                    txtJobsiteGFMIS.Text = "";
                    txtJobsiteName.Text = "";
                    txtJobsiteNameShort.Text = "";
                    txtJobsiteNameEng.Text = "";
                    txtNameEngShort.Text = "";
                    txtDateStart.SetValue("");
                    txtJobDescription.Text = "";
                    txtAddress.Text = "";
                    txtPostalCode.Text = "";
                    txtTel.Text = "";
                    txtFax.Text = "";
                    txtRemarks.Text = "";
                    txtWebsite.Text = "";

                    ddlWorkAreaSeq.SelectedValue = drc[0]["WORK_AREA_SEQ"].ToString(); //Convert.ToString(DataBinder.Eval(DataItem, "work_area_seq"));
                    ddlJobsiteUpperSeq.SelectedValue = drc[0]["JOB_SITE_SEQ"].ToString(); //Convert.ToString(DataBinder.Eval(DataItem, "upper_job_site_seq"));
                    if (ddlJobSiteRef.Visible)
                        ddlJobSiteRef.SelectedValue = "";

                    ddlJobsiteLevelSeq.SelectedValue = drc[0]["JOB_SITE_LEVEL_SEQ"].ToString();
                    ddlCountrySeq.SelectedValue = "";
                    LoadDDLCountryChange(ddlCountrySeq.SelectedValue);
                    LoadDDLProvinceSeq(ddlCountrySeq.SelectedValue);
                    cmbProvince.SelectedValue = "";
                    ddlDistrictSeq.SelectedValue = "";

                    ddlJobSiteHeadPosSeq.SelectedValue = "";


                    if (String.IsNullOrEmpty(ddlCountrySeq.SelectedValue)) // Initial
                    {
                        if (ddlCountrySeq.FindItemByText("ไทย") != null)
                        {
                            ddlCountrySeq.SelectedValue = ddlCountrySeq.FindItemByText("ไทย").Value;
                            LoadDDLCountryChange(ddlCountrySeq.SelectedValue);

                            LoadDDLProvinceSeq(ddlCountrySeq.SelectedValue);
                            if (cmbProvince.FindItemByText("กรุงเทพมหานคร") != null)
                            {
                                cmbProvince.SelectedValue = cmbProvince.FindItemByText("กรุงเทพมหานคร").Value;

                                LoadDDLDistrictSeq(cmbProvince.SelectedValue);
                            }

                        }

                    }
                }

            }
            if (ddlJobsiteLevelSeq.Visible)
                SetJobSiteLevelName(lblLevel.Text);
            else
                SetJobSiteLevelName(ddlJobsiteLevelSeq.Text);
        }

        private void LoadDetail()
        {
            DataSet ds = job.LoadJobSites(Session["TreeNodeValue"].ToString(), Request.QueryString["org"].ToString(), "", "");
            DataRowCollection drc;

            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    drc = ds.Tables[0].Rows;
                    lblName.Text = drc[0]["JOB_SITE_NAME"].ToString();// Info[0].Name;
                    lblRefNo.Text = drc[0]["JOB_SITE_REF"].ToString();// Info[0].RefNo;
                    lblUpperName.Text = drc[0]["UPPER_JOB_SITE_NAME"].ToString();// 
                    lblLevel.Text = drc[0]["JOB_SITE_LEVEL_NAME"].ToString();// Info[0].JobSiteLevels.Name;
                    lblGFMIS.Text = drc[0]["gfmis_code"].ToString();// Info[0].GFMIS;
                    lblNameShort.Text = drc[0]["JOB_SITE_NAME_SHORT"].ToString();// Info[0].NameShort;
                    lblNameEng.Text = drc[0]["JOB_SITE_NAME_ENG"].ToString();// Info[0].NameEng; 
                    lblNameEngShort.Text = drc[0]["JOB_SITE_NAME_ENG_SHORT"].ToString();// Info[0].NameEngShort;
                    lblStartDate.Text = ""; //string.Format("{0:dd/MMMM/yyyy}", drc[0]["DATE_START"]);
                    lblJobDescription.Text = drc[0]["JOB_DESCRIPTION"].ToString();// Info[0].JobDescription;
                    lblScope.Text = drc[0]["WORK_AREA_NAME"].ToString();// Info[0].WorkAreas.Name;
                    lblAddress.Text = drc[0]["ADDRESS"].ToString();// Info[0].Address.Address;
                    lblCountry.Text = drc[0]["COUNTRY_NAME"].ToString();// 
                    lblCity.Text = drc[0]["CITY_NAME"].ToString();
                    lblCountryZone.Text = drc[0]["ZONE_NAME"].ToString();// 
                    lblTel.Text = drc[0]["TEL"].ToString();// Info[0].Address.Phone;
                    lblFax.Text = drc[0]["FAX"].ToString();// Info[0].Address.Fax;
                    lblAddress2.Text = drc[0]["ADDRESS2"].ToString();// Info[0].Address.Phone;
                    lblWebSite.Text = drc[0]["WEBADDRESS"].ToString();// Info[0].Address.Fax;
                    lblRemarks.Text = drc[0]["REMARKS"].ToString();// Info[0].Remarks;
                    lblJobSiteHeadPos.Text = drc[0]["POS_NAME"].ToString();// Info[0].NameEng;
                    SetJobSiteLevelName(lblLevel.Text);
                }
                else
                    ClearDetail();
            }
            else
            {
                ClearDetail();
            }
            
        }
        private void ClearDetail()
        {
            lblName.Text = "";
            lblRefNo.Text = "";
            lblUpperName.Text = "";
            lblLevel.Text = "";
            lblGFMIS.Text = "";
            lblNameShort.Text = "";
            lblNameEng.Text = "";
            lblNameEngShort.Text = "";
            lblStartDate.Text = "";
            lblJobDescription.Text = "";
            lblScope.Text = "";
            lblAddress.Text = "";
            lblCountry.Text = "";
            lblCity.Text = "";
            lblCountryZone.Text = "";
            lblTel.Text = "";
            lblFax.Text = "";
            lblRemarks.Text = "";
            lblJobSiteHeadPos.Text = "";
            lblWebSite.Text = "";
            if (ddlJobsiteLevelSeq.Visible)
                SetJobSiteLevelName(ddlJobsiteLevelSeq.Text);
            else
                SetJobSiteLevelName(lblLevel.Text);
        }
        private void LoadTreeView()
        {
            Nullable<int> JobSiteTypeSeq = Int32.Parse(Request.QueryString["org"]);

            IList<JobSiteInfo> InfoTreeView = job.LoadJobSiteTreeView(null, JobSiteTypeSeq);
            //IList<TreeViewInfo> InfoTreeView = treeBLL.LoadJobSiteTreeView(Upper, JobSiteTypeSeq);
            RadTreeView1.DataSource = InfoTreeView;// job.SelectTreeView(Session[strSessionName].ToString(), Request.QueryString["IsInternal"]);//ไปเอา seq ของหน่วยงานเข้าของระบบ
            
            RadTreeView1.DataBind();

        }
        private void OnUpdateCmdUI(string Mode)
        {
            bool bViewMode = Mode == "VIEW";
            bool bAddNewMode = Mode == "ADDNEW";
            bool bEditMode = Mode == "EDIT";
            bool bNull = Mode == "";

            if (bViewMode)
                LoadDetail();

            PanelDetail.Visible = bViewMode;
            PanelEdit.Visible = !bViewMode && !bNull;
            //lblWarning.Visible = bNull;
            cmdAdd2.Visible = bAddNewMode;
            cmdUpdate2.Visible = bEditMode;
        }
        protected void cmdExpand_Click(object sender, EventArgs e)
        {
            RadTreeView1.ExpandAllNodes();
        }

        protected void cmdCollapse_Click(object sender, EventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }

        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            //LoadGrid();
            //lblHeader.Text = Session["HeaderName"].ToString() + " ภายใต้ " + RadTreeView1.SelectedNode.Text;
            
            Session["TreeNodeValue"] = RadTreeView1.SelectedNode.Value;
            if (Session["TreeNodeValue"].ToString() == "0")
                OnUpdateCmdUI("");
            else
            {
                OnUpdateCmdUI("VIEW");
                LoadDetail();
            }

            ClearControl();

            SetJobSiteLevelName(lblLevel.Text);
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            //string strXML = RadTreeView1.GetXml();
            RadTreeNodeCollection nodes = this.RadTreeView1.Nodes;
            string strSearch = "";// txtSearch.Text.TrimEnd();

            foreach (RadTreeNode n in nodes)
            {
                n.BackColor = RadTreeView1.BackColor;
                main.ClearRecursive(n, RadTreeView1.BackColor);
            }

            foreach (RadTreeNode n in nodes)
            {
                if (strSearch.Length > 0)
                {
                    if (n.Text.IndexOf(strSearch) >= 0)
                    {
                        n.BackColor = System.Drawing.Color.Yellow;
                        n.ExpandParentNodes();
                    }
                    main.FindRecursive(n, strSearch);
                }
            }
        }

        //protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        //{
        //    if (e.CommandName == "DeleteAll")
        //    {
        //        GridDataItem item;
        //        for (int i = 0; i < RadGrid1.Items.Count; i++)
        //        {
        //            item = RadGrid1.Items[i];
        //            if (item.Selected)
        //            {
        //                string strPK = item.OwnerTableView.DataKeyValues[i]["job_site_seq"].ToString();

        //                job.Delete(Int32.Parse(strPK), ssMng.EmpSeqI);
        //             }
        //        }

        //        LoadTreeView();

        //        if (Session["TreeNodeValue"] != null)
        //        {
        //            RadTreeView1.FindNodeByValue(Session["TreeNodeValue"].ToString()).Selected = true;
        //            RadTreeView1.SelectedNode.Expanded = true;
        //            RadTreeView1.SelectedNode.ExpandParentNodes();
        //            //lblHeader.Text = Session["HeaderName"].ToString() + " ภายใต้ " + RadTreeView1.SelectedNode.Text;
        //        }

        //    }
        //    else if (e.CommandName == "Delete")
        //    {
        //        JobSiteInfo Info = new JobSiteInfo();

        //        string strJobsiteSeq = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["job_site_seq"].ToString();

        //        job.Delete(Int32.Parse(strJobsiteSeq), ssMng.EmpSeqI);

        //        LoadTreeView();

        //        if (RadTreeView1.Nodes.Count > 0)
        //        {
        //            RadTreeView1.Nodes[0].Selected = true;
        //            Session["TreeNodeValue"] = RadTreeView1.Nodes[0].Value;
        //        }

        //    }
        //    else if (e.CommandName == "PerformInsert" || e.CommandName == "Update")
        //    {
        //        LoadTreeView();
        //        if (Session["TreeNodeValue"] != null)
        //        {
        //            RadTreeView1.FindNodeByValue(Session["TreeNodeValue"].ToString()).Selected = true;
        //            RadTreeView1.SelectedNode.Expanded = true;
        //            RadTreeView1.SelectedNode.ExpandParentNodes();
        //            //lblHeader.Text = Session["HeaderName"].ToString() + " ภายใต้ " + RadTreeView1.SelectedNode.Text;
        //        }

        //    }
        //    //if (e.CommandName == "Sort" || e.CommandName == "Page")
        //    //{
        //    //    RadToolTipManager1.TargetControls.Clear();
        //    //}

        //    //LoadGrid();
        //}
        //protected void OnAjaxUpdate(object sender, ToolTipUpdateEventArgs args)
        //{
        //    //this.UpdateToolTip(args.Value, args.UpdatePanel);
        //    this.UpdateToolTip(args.Value, args.UpdatePanel);
        //}
        //private void UpdateToolTip(string elementID, UpdatePanel panel)
        //{
        //    string strUserControlName = "../UserControls/MP_JobSiteDetails.ascx";

        //    Control ctrl = new Control();
        //    ctrl = Page.LoadControl(strUserControlName);
        //    panel.ContentTemplateContainer.Controls.Add(ctrl);
        //    Session["JobSiteSeq"] = elementID;
        //    //ProductDetails details = (ProductDetails)ctrl;
        //    //details.ProductID = elementID;
        //}
        
        protected void LoadDDLJobsiteUpperSeq()
        {
            //IList<JobSiteInfo> InfoList = job.ListJobSiteByTypes(Request.QueryString["org"]);
            DataTable dtb = job.ListJobSite(Request.QueryString["org"]);

            if (dtb.Rows.Count > 0)
            {
                ddlJobsiteUpperSeq.DataSource = dtb; // job.SelectDropDownList(strSystemJobsiteUpperSeq, strIsInternal);//ไปเอาหน่วยงานขึ้นตรงของเจ้าของระบบ จะไม่ error
                ddlJobsiteUpperSeq.DataBind();

                ddlJobSiteRef.DataSource = dtb; // job.SelectDropDownList(strSystemJobsiteUpperSeq, strIsInternal);//ไปเอาหน่วยงานขึ้นตรงของเจ้าของระบบ จะไม่ error
                ddlJobSiteRef.DataBind();
            }
        }
        //protected void LoadDDLJobsiteExternalSeq()
        //{
        //    IList<JobSiteInfo> InfoList = job.ListJobSiteByTypes(SystemInfo._JobsiteExternal);
        //    //DataSet ds = job.LoadJobSites("", SystemInfo._JobsiteExternal, "", "");
        //    //DataTable dt = new DataTable();

        //    if (InfoList.Count > 0)
        //    {
        //        ddlJobSiteRef.DataSource = InfoList; // job.SelectDropDownList(strSystemJobsiteUpperSeq, strIsInternal);//ไปเอาหน่วยงานขึ้นตรงของเจ้าของระบบ จะไม่ error
        //        ddlJobSiteRef.DataBind();
        //    }
        //}

        protected void LoadDDLWorkAreaSeq()
        {
            IList<WorkAreaInfo> Info = area.ListWorkAreas(null);
            ddlWorkAreaSeq.DataSource = Info;
            ddlWorkAreaSeq.DataBind();
        }
        protected void LoadDDLJobsiteLevelSeq()
        {
            IList<DropDownListInfo> infoList = ddlBLL.LoadListJobsiteLevels();
            ddlJobsiteLevelSeq.DataSource = infoList;
            ddlJobsiteLevelSeq.DataBind();
        }
        protected void LoadDDLJobsiteHeadPosSeq()
        {
            //var infoList = new List<PositionInfo> {new PositionInfo(null) {Name = "ไม่เลือก"}};
            //infoList.AddRange(positionBLL.ListPositionsNew());
            ddlJobSiteHeadPosSeq.DataSource = positionBLL.ListPositionsNew();
            ddlJobSiteHeadPosSeq.DataBind();
            ddlJobSiteHeadPosSeq.Items.Insert(0, new RadComboBoxItem("ไม่เลือก", ""));
            ddlJobSiteHeadPosSeq.SelectedIndex = 0;
        }
        protected void LoadDDLCountrySeq()
        {
            IList<DropDownListInfo> infoList = ddlBLL.LoadListCountry();
            ddlCountrySeq.DataSource = infoList;
            ddlCountrySeq.DataBind();

            ddlCountrySeq.Items.Insert(0, new RadComboBoxItem("-- เลือกประเทศ --", ""));
        }
        protected void LoadDDLCountryChange(string CountrySeq)
        {
            if (CountrySeq.Length > 0)
            {
                //IList<MasCountryInfo> countryList = countryBLL.ListMasCountries(CountrySeq, "");
                //if (countryInfo.CountryZones != null)
                //    cmbZone.SelectedValue = countryList[0].Sequence.ToString();

                //LoadDDLProvinceSeq(CountrySeq);
                lblProvinceCity.Text = CountrySeq.ToString() == SystemInfo._Local ? "จังหวัด" : "เมือง";
                LoadDDLProvinceSeq(CountrySeq);
            }
            else
            {
                LoadDDLProvinceSeq(null);
            }
        }
        protected void LoadDDLProvinceSeq(string Country)
        {
            cmbProvince.Items.Clear();

            if (!string.IsNullOrEmpty(Country))
            {
                if (Country == SystemInfo._Local)
                {
                    IList<DropDownListInfo> infoList = ddlBLL.LoadListProvinces(null);
                    cmbProvince.DataSource = infoList;
                }
                else
                {
                    IList<MasCityInfo> cityList = cityBLL.ListMasCities("", Country);
                    cmbProvince.DataSource = cityList;
                }

                cmbProvince.DataBind();

                cmbProvince.Items.Insert(0, new RadComboBoxItem("-- เลือกจังหวัด --", ""));
            }
        }
        protected void LoadDDLDistrictSeq(string provSeq)
        {
            ddlDistrictSeq.Items.Clear();

            if (!string.IsNullOrEmpty(provSeq))
            {
                IList<MasDistrictInfo> infoList = districtBLL.ListDistricts("", "", provSeq);
                IList<MasDistrictInfo> List = new List<MasDistrictInfo>();

                for (int i = 0; i < infoList.Count; i++)
                {
                    MasDistrictInfo c = new MasDistrictInfo();
                    c.Sequence = infoList[i].Sequence;
                    c.Name = "ตำบล" + infoList[i].Name + " อำเภอ" + infoList[i].Amphurs.Name;

                    List.Add(c);
                }
                ddlDistrictSeq.DataSource = List;
                ddlDistrictSeq.DataBind();

                ddlDistrictSeq.Items.Insert(0, new RadComboBoxItem("-- เลือกตำบล/อำเภอ --", ""));
            }
        }
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //{
            //    Control target = e.Item.FindControl("targetControl");
            //    if (!Object.Equals(target, null))
            //    {
            //        if (!Object.Equals(this.RadToolTipManager1, null))
            //        {
            //            //Add the button (target) id to the tooltip manager
            //            this.RadToolTipManager1.TargetControls.Add(target.ClientID, (e.Item as GridDataItem).GetDataKeyValue("job_site_seq").ToString(), true);

            //        }
            //    }
            //}            

        }

        protected void cmdAddNew_Click(object sender, EventArgs e)
        {
            OnUpdateCmdUI("ADDNEW");
            ClearControl();
        }

        protected void cmdEdit_Click(object sender, EventArgs e)
        {
            OnUpdateCmdUI("EDIT");
            BindDataControl();
        }

        protected void cmdDelete_Click(object sender, EventArgs e)
        {
            job.Delete(Int32.Parse(Session["TreeNodeValue"].ToString()), ssMng.EmpSeqI);
            LoadTreeView();
            LoadDDLJobsiteUpperSeq();
            OnUpdateCmdUI("");
        }

        protected void ddlCountrySeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDDLCountryChange(ddlCountrySeq.SelectedValue);
        }

        protected void cmbProvince_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDDLDistrictSeq(cmbProvince.SelectedValue);
        }

        protected void cmbProvince_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadDDLProvinceSeq(ddlCountrySeq.SelectedValue);
        }

        protected void ddlDistrictSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadDDLDistrictSeq(cmbProvince.SelectedValue);
        }

        protected void cmdAdd_Click(object sender, EventArgs e)
        {
            job.Insert(txtJobsiteRef.Text.Trim()
                , txtJobsiteName.Text
                , txtJobsiteNameShort.Text
                , txtJobsiteNameEng.Text
                , txtNameEngShort.Text
                , txtJobsiteGFMIS.Text.Trim()
                , txtJobDescription.Text
                , txtRemarks.Text
                , ssMng.EmpSeqI
                , txtDateStart.GetStringValue().Length > 0 ? DateTime.Parse(txtDateStart.GetStringValue()) : (DateTime?)null
                , null
                , txtAddress.Text
                , ddlDistrictSeq.SelectedValue//.Length > 0 ? Int32.Parse(ddlDistrictSeq.SelectedValue) : (int?)null
                , (ddlCountrySeq.SelectedValue == SystemInfo._Local ? "" : cmbProvince.SelectedValue)//.Length > 0 ? Int32.Parse(cmbProvince.SelectedValue) : (int?)null
                , ddlCountrySeq.SelectedValue
                , txtPostalCode.Text.Trim()
                , txtTel.Text.Trim()
                , txtFax.Text.Trim()
                , ddlJobsiteUpperSeq.SelectedValue.Length > 0 ? Int32.Parse(ddlJobsiteUpperSeq.SelectedValue) : (int?)null
                , Int32.Parse(Request.QueryString["org"])
                , Int32.Parse(ddlWorkAreaSeq.SelectedValue)
                , Int32.Parse(ddlJobsiteLevelSeq.SelectedValue)
                , ddlJobSiteRef.SelectedValue.Length > 0 ? Int32.Parse(ddlJobSiteRef.SelectedValue) : (int?)null
				, txtAddress2.Text.Trim()
				, txtWebsite.Text.Trim()
                , ddlJobSiteHeadPosSeq.SelectedValue);

            LoadTreeView();
            LoadDDLJobsiteUpperSeq();

            if (Session["TreeNodeValue"] != null)
            {
                RadTreeView1.FindNodeByValue(Session["TreeNodeValue"].ToString()).Selected = true;
                RadTreeView1.SelectedNode.Expanded = true;
                RadTreeView1.SelectedNode.ExpandParentNodes();
                //lblHeader.Text = Session["HeaderName"].ToString() + " ภายใต้ " + RadTreeView1.SelectedNode.Text;
            }
            OnUpdateCmdUI("VIEW");
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            job.Update(Int32.Parse(lblJobsiteSeq.Text.Trim())
                , txtJobsiteRef.Text.Trim()
                , txtJobsiteName.Text
                , txtJobsiteNameShort.Text
                , txtJobsiteNameEng.Text
                , txtNameEngShort.Text
                , txtJobsiteGFMIS.Text.Trim()
                , txtJobDescription.Text
                , txtRemarks.Text
                , ssMng.EmpSeqI
                , txtDateStart.GetStringValue().Length > 0 ? DateTime.Parse(txtDateStart.GetStringValue()) : (DateTime?)null
                , null
                , txtAddress.Text
                , ddlDistrictSeq.SelectedValue//.Length > 0 ? Int32.Parse(ddlDistrictSeq.SelectedValue) : (int?)null
                , (ddlCountrySeq.SelectedValue == SystemInfo._Local ? "" : cmbProvince.SelectedValue)
                , ddlCountrySeq.SelectedValue
                , txtPostalCode.Text.Trim()
                , txtTel.Text.Trim()
                , txtFax.Text.Trim()
                , ddlJobsiteUpperSeq.SelectedValue.Length > 0 ? Int32.Parse(ddlJobsiteUpperSeq.SelectedValue) : (int?)null
                , Int32.Parse(Request.QueryString["org"])
                , Int32.Parse(ddlWorkAreaSeq.SelectedValue)
                , Int32.Parse(ddlJobsiteLevelSeq.SelectedValue)
                , ddlJobSiteRef.SelectedValue.Length > 0 ? Int32.Parse(ddlJobSiteRef.SelectedValue) : (int?)null
				, txtAddress2.Text.Trim()
                , txtWebsite.Text.Trim()
                , ddlJobSiteHeadPosSeq.SelectedValue);

            LoadTreeView();
            LoadDDLJobsiteUpperSeq();

            if (Session["TreeNodeValue"] != null)
            {
                RadTreeView1.FindNodeByValue(Session["TreeNodeValue"].ToString()).Selected = true;
                RadTreeView1.SelectedNode.Expanded = true;
                RadTreeView1.SelectedNode.ExpandParentNodes();
                //lblHeader.Text = Session["HeaderName"].ToString() + " ภายใต้ " + RadTreeView1.SelectedNode.Text;
            }
            OnUpdateCmdUI("VIEW");
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            OnUpdateCmdUI("");
        }

        protected void cmdExpand_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void cmdCollapse_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ddlJobsiteLevelSeq_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            SetJobSiteLevelName(e.Text);
        }

        private void SetJobSiteLevelName(string t)
        {
            if (String.IsNullOrEmpty(t))
            {
                t = "หน่วยงาน";
            }
            t = "ชื่อ" + t;
            lblJobSiteLevelCaptionViewMode.Text = t;
            lblJobSiteLevelCaptionEditMode.Text = t;
        }
    }
}
